package com.vodafone.nocs.inops.tools.test.ldap;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import netscape.ldap.*;

public class LDAPLoader {

	String host = null;
	int port = 389;
	String authid = null;
	String authpw = null;
	String base = null;
	String filter = null;
	String[] ATTRS = { "cn", "mail", "telephonenumber" };
	public static LDAPConnection ld = null;


	public static void printUsage() {
		String str = null;
		str = "java LDAPSearchExample <host> <port> <authdn> <authpw> <baseDN> filename";
		System.out.println(str);
	}

	public String toString() {
		String str = null;

		str = "Host   : " + this.host + "\n" + "Port   : " + this.port + "\n"
				+ "AuthDN : " + this.authid + "\n" + "AuthPW : " + this.authpw
				+ "\n" + "BaseDN : " + this.base + "\n" + "Filter : "
				+ this.filter;
		return (str);
	}

	public static void main(String[] args) {

		if (args.length != 6) {
			printUsage();
			System.exit(1);
		}
		
		String host = args[0];
		int port = Integer.parseInt(args[1]);
		String authid = args[2];
		String authpw = args[3];
		String base = args[4];
		String fileName = args[5];
		
		LdapWrapper ldw = new LdapWrapper(host, port, authid, authpw, base);
		ldw.connectLdap();
		
		Jdispatcher myeng = new Jdispatcher(ldw);
		
		//XAN-IN-RDR3:/cust_use/home/ainet/gugs-> /usr/jdk/jdk1.6.0_26/bin/java -jar LDAPSearchExample.jar 127.0.0.1 1972  dview=XAN-IN-RDR3_request_data 123  dview=XAN-IN-RDR3_request_data "sid=OP=A;ACTION=IMOM#RMV:BUNDLE,MSISDN=447826679831,PTP_ID=FP_VF_D35,IDTYPE=S,ONLY_ACTIVE=N"
//		  361  /usr/jdk/jdk1.6.0_26/bin/java -jar LDAPSearchExample.jar 127.0.0.1 1972 dview=XAN-IN-RDR3_request_data 123 dview=XAN-IN-RDR3_request_data

//		String[] filter1 = {"sid=OP=A;ACTION=IMOM#SUB:DTPBUNDLE,MSISDN=447826679831,PTP_ID=FP_VF_D04,START_DATE=2014/03/01,END_DATE=2014/03/10,IDTYPE=S,PAYMENT_ID=EXTERNAL,BAL=P,ALLOW_SPD=N,UCL=N"};
//		String[] filter2 = {"sid=OP=A;ACTION=IMOM#SUB:DTPBUNDLE,MSISDN=447826679831,PTP_ID=FP_VF_C33,START_DATE=2014/03/01,END_DATE=2014/03/10,IDTYPE=S,PAYMENT_ID=EXTERNAL,BAL=P,ALLOW_SPD=N,UCL=N"};
//		String[] filter3 = {"sid=OP=A;ACTION=IMOM#SUB:DTPBUNDLE,MSISDN=447826679831,PTP_ID=FP_VF_S29,START_DATE=2014/03/01,END_DATE=2014/03/10,IDTYPE=S,PAYMENT_ID=EXTERNAL,BAL=P,ALLOW_SPD=N,UCL=N"};

//		String[] ldapExec  = {"sid=OP=Q;TBL_NAME=CTRTDB;TBL_KEY=447826679831:0:1:ALL:1;"};
//		String[] ldapExec1 = {"sid=OP=Q;TBL_NAME=CTRTDB;TBL_KEY=447826679831:0:1:ALL:1;"};
//		String[] ldapExec2 = {"sid=OP=Q;TBL_NAME=CTRTDB;TBL_KEY=447826679831:0:1:ALL:1;"};
//		String[] ldapExec3 = {"sid=OP=Q;TBL_NAME=CTRTDB;TBL_KEY=447826679831:0:1:ALL:1;"};
//
//		while(true){
//			myeng.p_w(ldapExec);
//			myeng.p_w(ldapExec1);
//			myeng.p_w(ldapExec2);
//			myeng.p_w(ldapExec3);
//		}
		
		BufferedReader br = null;
		try {
			String sCurrentLine;
			br = new BufferedReader(new FileReader(fileName));
			while ((sCurrentLine = br.readLine()) != null) {
				if (sCurrentLine.startsWith("1")){
					String[] toParse = new String[1];
					toParse[0] = sCurrentLine.substring(1);
					myeng.p_w(toParse);
				}else if (sCurrentLine.startsWith("2") ){
					String[] toParse = new String[2];
					toParse[0] = sCurrentLine.substring(1);
					toParse[1] = br.readLine();
					myeng.p_w(toParse);
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}


		//do not delete
//		try {
//			System.out.println("waiting");
//			Thread.sleep(10000);
//		} catch (InterruptedException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		if ((ld != null) && ld.isConnected()) {
//			try {
//				ld.disconnect();
//			} catch (LDAPException e) {
//				System.out.println(e.toString());
//			}
//		}
//
//		
//		return;
	}

}